package com.sxpi.course.controller;

import cn.hutool.core.map.MapUtil;
import com.sxpi.common.result.R;
import com.sxpi.course.pojo.CoursePrise;
import com.sxpi.course.service.coursePrise.ICoursePriseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author: 董伟豪
 * @Date: 2022/11/15 11:08
 */
@RestController
@Slf4j
public class CoursePriseController {

    @Autowired
    private ICoursePriseService coursePriseService;

    /**
     * 保存用户点赞信息 redisson锁
     * @param coursePrise
     * @return
     */
    @PostMapping("/course/parise/savelock")
    public R insertCoursePriseLock(@RequestBody CoursePrise coursePrise) {
        int i = coursePriseService.insertCoursePriseRedisLock(coursePrise);
        return R.ok().put("flag", i > 0);
    }

    /**
     * 保存用户点赞信息
     * @param coursePrise
     * @return
     */
    @PostMapping("/course/parise/save")
    public R insertCoursePrise(@RequestBody CoursePrise coursePrise) {
        int i = coursePriseService.insertCoursePrise(coursePrise);
        return R.ok().put("flag", i > 0);
    }

    /**
     * 查看某个用户的课程点赞记录
     * @param courseId
     * @param userId
     * @return
     */
    @PostMapping("/course/parise/select/{courseId}/{userId}")
    public R selectByCourseIdAndUserId(@PathVariable("courseId") Long courseId, @PathVariable("userId") Long userId) {
        CoursePrise coursePrise = coursePriseService.selectByCourseIdAndUserId(courseId, userId);
        return R.ok().put("coursePrise", coursePrise);
    }


    /**
     * 根据课程id查询总的点赞记录
     * @param courseId
     * @return
     */
    @PostMapping("/course/parise/count/{courseId}")
    public R countByCourseId(@PathVariable("courseId") Long courseId) {
        int count = coursePriseService.countByCourseId(courseId);
        return R.ok().put("count", count);
    }

    /**
     * 用户取消课程的点赞记录
     * @param courseId
     * @param userId
     * @return
     */
    @PostMapping("/course/parise/cancel/{courseId}/{userId}")
    public R cancelCoursePrise(@PathVariable("courseId") Long courseId, @PathVariable("userId") Long userId) {
        int i = coursePriseService.cancelCoursePrise(courseId, userId);
        return R.ok().put("flag", i > 0);
    }

    /**
     * 获取课程的点赞的总数排行
     * @return
     */
    @PostMapping("/course/parise/rank")
    public R getCoursePriseRank() {
        List<Map<String, Object>> coursePriseRank = coursePriseService.getCoursePriseRank();
        return R.ok().put("ranks", coursePriseRank);
    }
}
